Regular Expressions

Regular expressions is a general term which covers the idea of pattern searching, typically in a string (or a vector of strings).

For now we'll learn about two useful functions for regular expressions and pattern searching (we'll go deeper into this topic in general later on):

  • grepl(), which returns a logical indicating if the pattern was found

  • grep(), which returns a vector of index locations of matching pattern instances

For both of these functions you'll pass in a pattern and then the object you want to search. Let's see some quick examples:

In [1]:
text <- "Hi there, do you know who you are voting for?"
In [4]:
grepl('voting',text)
Out[4]:
TRUE
In [5]:
grepl('Hi',text)
Out[5]:
TRUE
In [6]:
grepl('Sammy',text)
Out[6]:
FALSE
In [8]:
v <- c('a','b','c','d')
In [9]:
grep('a',v)
Out[9]:
1
In [10]:
grep('c',v)
Out[10]:
3

We'll learn more regular expression functions as we need them when doing exercises or projects. Want more info on regular expressions with R in the meantime? Check out this link